<div class="layuimini-container layuimini-page-anim" divname="DeviceLog">
    <div class="layuimini-main">

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="search"> <i class="layui-icon"></i> 查询 </button>

                <button class="layui-btn layui-btn-sm" lay-event="refresh"> 刷新 </button>

                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="clear"> 清空日志 </button>
            </div>
        </script>

        <table class="layui-hide" id="DeviceLogPage" lay-filter="DeviceLogPageTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">详情</a>
        </script>
    </div>
</div>

<script>


    layui.use(['form', 'table', 'miniPage', 'element', 'faceAPI'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;
        var faceAPI = layui.faceAPI;
        form.render();

        IniSearchPar();

        table.render({
            elem: '#DeviceLogPage',
            //**************************异步请求开始***********************
            url: 'api/FaceIOLog/GetLogs',
            method: 'post',
            where: faceAPI.DeviceLogSearchPageResult.field,
            headers: {
                Authorization: "Bearer " + faceAPI.GetJWTToken()
            },//增加Token验证头
            request: {
                pageName: 'pageIndex', //页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            parseData: function (res) { //res 即为原始返回的数据
                if (res.errCode == 0) {
                    var data = res.content;
                    var ret = {
                        "code": res.errCode, //解析接口状态
                        "msg": res.error, //解析提示文本
                        "count": data.totalCount, //解析数据长度
                        "data": data.dataList //解析数据列表
                    };
                    return ret;

                } else {

                    return {
                        "code": 0, //解析接口状态
                        "msg": '', //解析提示文本
                        "count": 0, //解析数据长度
                        "data": [] //解析数据列表
                    };
                }

            },
            //**************************异步请求结束***********************

            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                { field: 'createTime', width: 170, title: '时间', sort: true },
                { field: 'deviceSN', width: 170, title: '设备SN', sort: true },
                { field: 'method', width: 170, title: '方式', sort: true },
                { field: 'body', title: '请求参数', sort: true },
                { field: 'ipAddr', title: '请求地址', width: 170, sort: true },
                {
                    field: 'result', width: 100, title: '特征信息长度', sort: true, templet: function (d) {
                        if (d.fileSize == 0) {
                            return "无";
                        }
                        return d.fileSize + "字节";

                    }
                },
                { title: '操作', width: 80, toolbar: '#currentTableBar', align: "center" }
            ]],
            limits: [100, 200, 300, 400, 500, 600],
            limit: 100,
            page: true,
            skin: 'line',
            done: function (res, curr, count) {
                var that = this.elem.next();
                res.data.forEach(function (item, index) {
                    //  debugger
                    var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                    var color = "Red";
                    switch (item.method) {
                        case "命令完成":
                        case "连接设备成功":
                            color = "Green";
                            break;
                        case "发送数据":
                            color = "Blue";
                            break;
                        case "接收数据":
                            color = "Indigo";
                            break;
                        case "命令进度":
                            color = "Violet";
                            break;
                        default:
                            color = "Red";
                    }
                    tr.find(".laytable-cell-1-0-2").css("color", color);
                    tr.find(".laytable-cell-1-0-3").css("color", color);
                });
            }
        });





        /**
         * toolbar事件监听
         */
        table.on('toolbar(DeviceLogPageTableFilter)', function (obj) {
            if (obj.event === 'search') {  // 查询操作
                var content = miniPage.getHrefContent('page/DeviceLog/search.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '搜索日志',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end: function (layero, index) {

                        var rst = faceAPI.DeviceLogSearchPageResult;
                        if (rst != null && rst.result) {
                            refreshTable(false);

                        };

                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            else if (obj.event == 'refresh') {
                //执行搜索重载
                refreshTable(true);
            }
            else if (obj.event == 'clear') {
                layer.confirm('操作确认：清空通讯日志？', function (index) {

                    faceAPI.Ajax({
                        type: 'Delete',
                        url: '/api/FaceIOLog/DeleteLogs',
                        success: function (result) {
                            if (result.result == false) {
                                layer.alert(result.error, { icon: 2 });
                            }
                            else {
                                refreshTable(true);
                            }

                        }
                    });


                    layer.close(index);
                });


            }
        });
        function refreshTable(bIni) {
            if (bIni) IniSearchPar();
            var rst = faceAPI.DeviceLogSearchPageResult;
            table.reload('DeviceLogPage', {
                page: {
                    curr: 1
                }, where: rst.field
            }, 'data');
        }
        function IniSearchPar() {
            var par = {
                field: {
                    logTimeBegin: dayjs().subtract(7, 'day').format("YYYY-MM-DD") + ' 00:00:00',
                    logTimeEnd: dayjs().format("YYYY-MM-DD") + ' 23:59:59',
                },
                result: true
            };

            faceAPI.DeviceLogSearchPageResult = par;
        }

        table.on('tool(DeviceLogPageTableFilter)', function (obj) {
            var data = obj.data;

            if (obj.event === 'edit') {

                var content = miniPage.getHrefContent('page/DeviceLog/Detail.html');
                var openWH = miniPage.getOpenWidthHeight();
                faceAPI.DeviceLogDetail = data;//向layer页面传值，传值主要代码
                var index = layer.open({
                    title: '日志详情',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }

        });



    });



</script>